package teaMainFrame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rORM.core.MysqlQuery;
import com.rORM.core.Query;
import com.rORM.core.QueryFactory;

import database.LessonTable;
import database.Score;
import database.Student;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ScoreTable extends JFrame {
	Score st = new Score();
	Student s = new Student();
	LessonTable l = new LessonTable();
	private JPanel contentPane;
	private JTextField inputNum;
	private JTextField inputcourseNum;
	private JTextField inputScore;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ScoreTable frame = new ScoreTable();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ScoreTable() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(695, 419);
		setLocationRelativeTo(getOwner());
		setResizable(false);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("新建成绩表");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		lblNewLabel.setBounds(167, 17, 89, 32);
		contentPane.add(lblNewLabel);
		
		JLabel label = new JLabel("请输入学号");
		label.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		label.setBounds(42, 74, 89, 32);
		contentPane.add(label);
		
		JLabel lblNewLabel_1 = new JLabel(s.getName());
		lblNewLabel_1.setBounds(276, 83, 61, 16);
		contentPane.add(lblNewLabel_1);
		
		JLabel label_1 = new JLabel("请输入课程号");
		label_1.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		label_1.setBounds(42, 138, 102, 32);
		contentPane.add(label_1);
		
		inputNum = new JTextField();
		inputNum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Student s = InputStuInfo(Integer.parseInt(inputNum.getText()));
				lblNewLabel_1.setText(s.getName());
			}
		});
		inputNum.setBounds(143, 78, 89, 26);
		contentPane.add(inputNum);
		inputNum.setColumns(10);
		
		
		
		
		JLabel lblNewLabel_2 = new JLabel(l.getLessonName());
		lblNewLabel_2.setBounds(276, 147, 61, 16);
		contentPane.add(lblNewLabel_2);
		
		JLabel label_2 = new JLabel("请输入成绩");
		label_2.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		label_2.setBounds(42, 198, 102, 32);
		contentPane.add(label_2);
		
		inputcourseNum = new JTextField();
		inputcourseNum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LessonTable l = InputLessonTable(Integer.parseInt(inputcourseNum.getText()));
				lblNewLabel_2.setText(l.getLessonName());
			}
		});
		inputcourseNum.setColumns(10);
		inputcourseNum.setBounds(143, 138, 89, 26);
		contentPane.add(inputcourseNum);
		
		
		
		inputScore = new JTextField();
		inputScore.setColumns(10);
		inputScore.setBounds(143, 202, 89, 26);
		contentPane.add(inputScore);
		
		JButton commit = new JButton("提交");
		commit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				st.setScore(Integer.parseInt(inputScore.getText()));
				st.setStuId(Integer.parseInt(inputNum.getText()));
				st.setCourseNum(Integer.parseInt(inputcourseNum.getText()));
				InputScoreTable(st);
				JOptionPane.showMessageDialog(null,"创建成功","信息提示框",JOptionPane.INFORMATION_MESSAGE);
				setVisible(false);
			}
		});
		commit.setBounds(351, 232, 75, 29);
		contentPane.add(commit);
		
		JButton next = new JButton("下一个");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				st.setScore(Integer.parseInt(inputScore.getText()));
				st.setStuId(Integer.parseInt(inputNum.getText()));
				st.setCourseNum(Integer.parseInt(inputcourseNum.getText()));
				InputScoreTable(st);
				new ScoreTable();
				setVisible(false);
			}
		});
		next.setBounds(250, 232, 75, 29);
		contentPane.add(next);
		
		JButton back = new JButton("返回");
		back.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				
			}
		});
		back.setBounds(6, 242, 75, 29);
		contentPane.add(back);
	}
	public Student InputStuInfo(int id){
		Query q = QueryFactory.createQuery();
		Student s = (Student) q.queryUniqueRow("select * from student where id  = ?",Student.class, new Object[]{id});
		return s;
	}
	public LessonTable InputLessonTable(int lessonid){
		Query q = QueryFactory.createQuery();
		LessonTable l = (LessonTable) q.queryUniqueRow("select * from LessonTable where lessonID  = ?",LessonTable.class, new Object[]{lessonid});
		return l;
	}
	public void InputScoreTable(Score s){
		new MysqlQuery().insert(s);
	}
}
